//
//  MessageDataSource.m
//  tcpoctest
//
//  Created by apple on 13-7-11.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "MessageDataSource.h"
#import "XMMessageInfo.h"

@implementation MessageDataSource

-(id)init
{
    self = [super init];
    if(self != nil)
    {
        MessageArr = [[NSMutableArray alloc] init];
        MessageDic = [[NSMutableDictionary alloc] init];
        pthread_mutex_init(&mutexMessage, 0);
    }
    
    return self;
}

-(int)GetMagArrCount
{
    int icount = 0;
    
    pthread_mutex_lock(&mutexMessage);
    
    icount = MessageArr.count;
    
    pthread_mutex_unlock(&mutexMessage);
    
    return icount;
}

-(void)DelAllMsgArr
{
    pthread_mutex_lock(&mutexMessage);
    
    [MessageArr removeAllObjects];
    [MessageDic removeAllObjects];
    
    pthread_mutex_unlock(&mutexMessage);
}

-(void)addMsg:(XMMessageInfo *)xmminfo
{
    pthread_mutex_lock(&mutexMessage);
    
    if (xmminfo != nil) {
        if([MessageDic objectForKey:xmminfo.strmsgid] == nil) {
            [MessageArr addObject:xmminfo.strmsgid];
        }
        
        [MessageDic setObject:xmminfo forKey:xmminfo.strmsgid];
    }
    
    
    pthread_mutex_unlock(&mutexMessage);
}

-(void)DelMsg:(XMMessageInfo *)xmminfo
{
    pthread_mutex_lock(&mutexMessage);
    
    [MessageArr removeObject:xmminfo.strmsgid];
    [MessageDic removeObjectForKey:xmminfo.strmsgid];
    
    pthread_mutex_unlock(&mutexMessage);
}

-(XMMessageInfo *)getMsgArrObject:(int)iindex
{
    XMMessageInfo *xmminfo = nil;
    
    pthread_mutex_lock(&mutexMessage);
    
    if (iindex >= 0 && iindex < MessageArr.count) {
        NSString *strid = [MessageArr objectAtIndex:iindex];
        xmminfo = [MessageDic objectForKey:strid];
    }
    
    pthread_mutex_unlock(&mutexMessage);
    
    return xmminfo;
}

-(NSString *)getMsgID
{
    NSString *strid = [NSString stringWithFormat:@"%d",MessageDic.count];
    
    NSArray *keyarr = [MessageDic allKeys];
    
    int iindex = 1;
    while ([keyarr containsObject:strid]) {
        strid = [NSString stringWithFormat:@"%d",MessageDic.count + iindex];
        ++iindex;
    }
    
    return strid;
}

-(XMMessageInfo *)getMsgInfoByID:(NSString *)strid
{
    XMMessageInfo *xmminfo = nil;
    
    pthread_mutex_lock(&mutexMessage);
    
    xmminfo = [MessageDic objectForKey:strid];
    
    pthread_mutex_unlock(&mutexMessage);
    
    return xmminfo;
}

-(void)MsgSortArr
{
    
    [MessageArr sortUsingFunction:customSortMsg context:nil];
}

NSInteger customSortMsg(id obj1, id obj2,void* context)
{
    NSString* obj_1 = (NSString*)obj1;
    NSString* obj_2 = (NSString*)obj2;
    
    NSComparisonResult res = (NSComparisonResult)NSOrderedSame;
    
    do {
        
        XMMessageInfo *ginfo1 = [[DataControl shareControl].messageDS getMsgInfoByID:obj_1];;
        XMMessageInfo *ginfo2 = [[DataControl shareControl].messageDS getMsgInfoByID:obj_2];
        
        if (ginfo1 == nil || ginfo1 == nil) {
            break;
        }
        
        if (ginfo1.mbAlreadayArgee && !ginfo2.mbAlreadayArgee) {
            res = (NSComparisonResult)NSOrderedDescending;
        }
        else if(!ginfo1.mbAlreadayArgee && ginfo2.mbAlreadayArgee)
        {
            res = (NSComparisonResult)NSOrderedAscending;
        }
        else
        {
            long long array = [ginfo1.strtime floatValue];
            long long array1 = [ginfo2.strtime floatValue];
            
            
                
                if(array - array1 > 0)
                {
                    res = (NSComparisonResult)NSOrderedAscending;
                    break;
                }
                else if(array - array1 < 0)
                {
                    res = (NSComparisonResult)NSOrderedDescending;
                    break;
                }
 
        }
        
    } while (false);
    
    return res;
}

@end
